package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Logup extends Activity {

    Button Logup;
    EditText UserName;
    EditText PassWord;
    TextView tip;
    public static final String sqlUserName="root";
    public static final String sqlPassWord="tzyTZY123";

    public static final String Url="jdbc:mysql://cdb-l1c6g0ne.bj.tencentcdb.com:10166/AndroidUser?user=root&password=tzyTZY123";
    public static final String Driver="com.mysql.jdbc.Driver";
    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logup);
        tip=(TextView)findViewById(R.id.loguptip) ;
        Logup=(Button)findViewById(R.id.LogUpButton);
        UserName=(EditText)findViewById(R.id.LogupUserName);
        PassWord=(EditText)findViewById(R.id.LogupPassWord);
    }
    public static boolean ReigsterVerify(String UserName,String PassWord) throws ClassNotFoundException, SQLException, SQLException {//注册成功为true，反之为false
        ArrayList<String> password = new ArrayList<>();
        Class.forName(Driver);
        //2.创建连接
        connection = DriverManager.getConnection(Url);
        String MySql = "select * from android_user where UserName=?";//?代表参数会在以后加
        preparedStatement = connection.prepareStatement(MySql);
        preparedStatement.setString(1, UserName);//代表第一个？放置这个参数
        resultSet =  preparedStatement.executeQuery();
        //遍历查询结果集
        password=new ArrayList<>();
        while(resultSet.next()){
            password.add(resultSet.getString("PassWord"));
        }
        if (password.size()==0){//没有查找该用户,则可以创建
            String MySql2 = "insert into android_user(UserName,PassWord) values(?,?)";//插入数据
            preparedStatement = connection.prepareStatement(MySql2);
            preparedStatement.setString(1, UserName);
            preparedStatement.setString(2, PassWord);
            if(preparedStatement.executeUpdate()!=0){
                return true;
            }
            else {
                return false;//插入失败
            }
        }
        else{
            return false;
        }

    }

    public void onClickLogup(View view) throws ClassNotFoundException, SQLException {//登录
        System.out.println(UserName.getText());
        System.out.println(PassWord.getText());
        Editable theUserName=UserName.getText();
        Editable thePassWord=PassWord.getText();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if(ReigsterVerify(theUserName.toString(),thePassWord.toString())){
                startActivity(new Intent(Logup.this,LoginWithanim.class));
            }
            else {
                tip.setText("注册失败");
            }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }).start();
//        if(ReigsterVerify(theUserName.toString(),thePassWord.toString())){
//            startActivity(new Intent(Logup.this,SelectItem.class));
//        }
//        else {
//            tip.setText("注册失败");
//        }





//        new Thread(new Runnable(){
//            @Override
//            public void run() {
//                try {
//                    if(LoginVerify(theUserName.toString(),thePassWord.toString())){
//                        startActivity(new Intent(Login.this,SelectItem.class));
//                    }
//                    else {
//                        tip.setText("PassWord Error");
//
//                    }
//                    System.out.println(LoginVerify(theUserName.toString(),thePassWord.toString()));
//                } catch (ClassNotFoundException e) {
//                    e.printStackTrace();
//                }
//
//            }
//        }).start();
    }
}